package com.fzn3.day03;

import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;

import java.util.Arrays;

/**
 * @author fzn
 * 描述：
 * @program DataStructure
 * @create 2023/3/9 9:54
 **/
public class TestSinglyLinkedListSentinel {

    private static SinglyLinkedListSentinel getLinkedList() {
        SinglyLinkedListSentinel singlyLinkedListSentinel = new SinglyLinkedListSentinel();
        singlyLinkedListSentinel.addLast(1);
        singlyLinkedListSentinel.addLast(2);
        singlyLinkedListSentinel.addLast(3);
        singlyLinkedListSentinel.addLast(4);
        return singlyLinkedListSentinel;
    }

    @Test
    @DisplayName("遍历")
    public void test1() {
        SinglyLinkedListSentinel singlyLinkedListSentinel = getLinkedList();

//        singlyLinkedListSentinel.loop1();
//        singlyLinkedListSentinel.loop2(value->{
//            System.out.print(value+" ");
//        });
        singlyLinkedListSentinel.loop3(value -> {
            System.out.print(value + " ");
        });
    }

    @Test
    @DisplayName("增强for")
    public void test2() {
        SinglyLinkedListSentinel singlyLinkedListSentinel = getLinkedList();
        for (Integer value : singlyLinkedListSentinel) {
            System.out.print(value + " ");
        }
        System.out.println();
    }


    @Test
    @DisplayName("addLast")
    public void test3() {
        SinglyLinkedListSentinel singlyLinkedListSentinel = getLinkedList();
        Assertions.assertIterableEquals(Arrays.asList(1, 2, 3, 4), singlyLinkedListSentinel);
    }

    @Test
    @DisplayName("get")
    public void test4() {
        SinglyLinkedListSentinel singlyLinkedListSentinel = getLinkedList();
//        System.out.println(singlyLinkedList.get(2));
        Assertions.assertEquals(2, singlyLinkedListSentinel.get(1));
        Assertions.assertThrows(IllegalArgumentException.class, () -> singlyLinkedListSentinel.get(10));
    }

    @Test
    @DisplayName("insert")
    public void test5() {
        SinglyLinkedListSentinel singlyLinkedListSentinel = getLinkedList();

        singlyLinkedListSentinel.insert(0, 5);
//        for (Integer value : singlyLinkedListSentinel) {
//            System.out.print(value + " ");
//        }
//        System.out.println();
        Assertions.assertIterableEquals(Arrays.asList(5, 1, 2, 3, 4), singlyLinkedListSentinel);
        singlyLinkedListSentinel.addFirst(6);
        Assertions.assertIterableEquals(Arrays.asList(6, 5, 1, 2, 3, 4), singlyLinkedListSentinel);

        SinglyLinkedListSentinel list = new SinglyLinkedListSentinel();
        list.addFirst(1);
        Assertions.assertIterableEquals(Arrays.asList(1), list);
    }

    @Test
    @DisplayName("removeFirst")
    public void test6() {
        SinglyLinkedListSentinel singlyLinkedListSentinel = getLinkedList();

        singlyLinkedListSentinel.removeFirst();
        Assertions.assertIterableEquals(Arrays.asList(2, 3, 4), singlyLinkedListSentinel);
        singlyLinkedListSentinel.removeFirst();
        Assertions.assertIterableEquals(Arrays.asList(3, 4), singlyLinkedListSentinel);
        singlyLinkedListSentinel.removeFirst();
        Assertions.assertIterableEquals(Arrays.asList(4), singlyLinkedListSentinel);
        singlyLinkedListSentinel.removeFirst();
        Assertions.assertIterableEquals(Arrays.asList(), singlyLinkedListSentinel);
        Assertions.assertThrows(IllegalArgumentException.class, singlyLinkedListSentinel::removeFirst);
    }

    @Test
    @DisplayName("remove")
    public void test7() {
        SinglyLinkedListSentinel singlyLinkedListSentinel = getLinkedList();

        singlyLinkedListSentinel.remove(3);
//        for (Integer value : singlyLinkedListSentinel) {
//            System.out.print(value + " ");
//        }
//        System.out.println();
        Assertions.assertIterableEquals(Arrays.asList(1, 2, 3), singlyLinkedListSentinel);
    }

}
